/* eslint-disable no-case-declarations */

import { picture } from '@/components/picture';
import { Rate } from 'ant-design-vue';
import { isArray } from '@/utils/is';
import { h } from 'vue';

export const useCustColumns = (dataSoucre: Recordable[]) => {
    if(dataSoucre && dataSoucre.length > 0){
        const custColumns = dataSoucre.filter((v: Recordable) => v?._other_?.isShowList);
        return custColumns.map((item: Recordable) => {
            const { name, _config_, _props_ }:Recordable = item;
            const column:Recordable = { title: name, dataIndex: _config_?.field };
            // 根据不同的组件处理不同的显示数据
            switch (_config_?.component) {
                case 'Select':
                    column.customRender = ({ value }) => {
                        if(value) {
                            if(isArray(value)){
                                const values:any = [];
                                if(value.length){
                                    value.forEach((key: any) => {
                                        const options = _props_?.options || [];
                                        const item = options.find((item: Recordable) => item?.value == key);
                                        if(item){
                                            values.push(item?.label);
                                        }
                                    });
                                }
                                return values.join('，') || '';
                            }else{
                                const options = _props_?.options || [];
                                const item = options.find((item: Recordable) => item?.value == value);
                                return item?.label || '';
                            }
                        }
                        return '';
                        
                        
                    };
                break;
                case 'RadioGroup':
                    column.dataFormat = { type: 'text', options: _props_?.options };
                break;
                case 'CheckboxGroup':
                    column.customRender = ({ value }) => {
                        const values:any = [];
                        if(value && value.length){
                            value.forEach((key: any) => {
                                const options = _props_?.options || [];
                                const item = options.find((item: Recordable) => item?.value == key);
                                if(item){
                                    values.push(item?.label);
                                }
                            });
                        }
                        return values.join(',') || '';
                    };
                break;
                case 'Switch':
                    column.dataFormat = { type: 'Tag', options: [{ value: 0, label: '否', color: 'red' }, { value: 1, label: '是', color: 'green' }] };
                break;
                case 'Rate':
                    column.customRender = ({ value }) => {
                        return h(Rate, {
                            value,
                            count: _props_?.count,
                            disabled: true,
                        });
                    };
                break;
                case 'UploadImg':
                    column.customRender = ({ value }) => {
                        if(value && value.length > 0){
                            const [file] = value;
                            const imgList = value.map((item: Recordable) => {
                                const imgURL = item?.url;
                                return imgURL;
                            });
                            return h(picture, { src: file?.url, files: imgList, width: 40, height: 40 });
                        }
                        return '';
                    };
                    column.width = 100;
                break;
            
                default:
                    break;
            }
            return column;
        });
    }
    return [];
};